<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 我的收藏 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="bookId" placeholder="书籍编号" class="handle-input mr10" style="width: 100px"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="bookList"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column prop="type" label="分类"></el-table-column>
                <el-table-column prop="description" label="简介"></el-table-column>
                <el-table-column label="作者">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="showAuthor(scope.row.authorId)">查看作者信息</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="publisher" label="出版社"></el-table-column>
                <el-table-column prop="publishDate" label="出版日期"></el-table-column>

                <el-table-column label="封面(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.coverUrl" :preview-src-list="[scope.row.coverUrl]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-discount" @click="borrowBook(scope)" v-if="scope.row.isBorrow == 0"
                            >借阅书籍</el-button
                        >
                        <el-tag type="info" v-if="scope.row.isBorrow == 1">书籍已被借阅</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="info" @click="collectOrNotBook(scope)"> 取消收藏 </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 作者信息弹出框 -->
        <el-dialog title="作者信息" :visible.sync="showAuthorVisible" width="30%">
            <el-form ref="form" label-width="70px">
                <el-form-item label="编号">
                    {{ book.author.id }}
                </el-form-item>
                <el-form-item label="姓名">
                    {{ book.author.name }}
                </el-form-item>
                <el-form-item label="性别">
                    {{ book.author.sex == 1 ? '男' : '女' }}
                </el-form-item>
                <el-form-item label="出生日期">
                    {{ book.author.birthday }}
                </el-form-item>
                <el-form-item label="简介">
                    {{ book.author.description }}
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="showAuthorVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            bookId: '',
            typeList: [],
            bookList: [],
            book: {
                id: '',
                title: '',
                type: '',
                description: '',
                authorId: '',
                publisher: '',
                author: []
            },
            showAuthorVisible: false,
            multipleSelection: [],
            idx: -1
        };
    },
    created() {
        this.handleSearch();
    },
    methods: {
        // 触发搜索按钮
        handleSearch() {
            this.$http.get('/book/mycollect').then((res) => {
                this.bookList = res.data.data;
            });
        },

        showAuthor(authorId) {
            console.log('打开作者信息');
            this.$http.get('/author/detail/' + authorId).then((res) => {
                this.book.author = res.data.data;
                this.showAuthorVisible = true;
            });
        },
        borrowBook(scope) {
            var book = scope.row;
            this.$http.get('book/borrow/' + book.id).then((res) => {
                if (res.data.status == 1) {
                    book.isBorrow = 1;
                    this.$set(this.bookList, scope.$index, book);
                    this.$message.success('书籍借阅成功');
                    this.book.isBorrow = 1;
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        collectOrNotBook(scope){
            this.$http.post("/book/collectOrNot/"+scope.row.id).then(res => {
                if(res.data.status==1){
                    this.bookList.splice(scope.$index,1);
                    this.$message.warning(res.data.message);
                }else{
                    this.$message.error("操作失败");
                }
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
